package com.sailing.face.service.impl;

import com.sailing.face.service.FileManagerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author fox 2017年11月7日17:08:42
 */
@Service
public class FileManagerServiceImpl implements FileManagerService {

    private static Logger logger = LoggerFactory
            .getLogger(FileManagerServiceImpl.class);
    @Value("${file.collect.path}")
    private String faceFilePath;

    @Override
    public void downloadPicture(String fileName, HttpServletResponse response) {
        logger.info("开始下载图片 fileName={}", fileName);
        ByteArrayInputStream bis = null;
        BufferedOutputStream bos = null;
        byte[] binary = null;
        try {

            FileInputStream inputStream = new FileInputStream(faceFilePath+"/"+fileName);
            binary = readInputStream(inputStream);

        } catch (Exception e) {

            logger.error("error--------------------文件读取错误------------------------------------------");
            e.printStackTrace();
        }
        if (binary != null && binary.length > 0) {
            try {
                logger.info("下载照片--------------------------写出流数据 开始------------------------------");

                response.setHeader("Accept-Ranges", "bytes");
                response.setHeader("Content-Type", "image/jpg");
                response.setHeader("Content-disposition",
                        "attachment; filename="+fileName);
                response.setHeader("Content-Length",
                        String.valueOf(binary.length));
                bis = new ByteArrayInputStream(binary);
                bos = new BufferedOutputStream(response.getOutputStream());
                byte[] buff = new byte[2048];
                int bytesRead;
                while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                    bos.write(buff, 0, bytesRead);
                }
                logger.info("下载照片--------------------------写出流数据 结束------------------------------");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bis != null)
                        bis.close();
                    if (bos != null)
                        bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        } else {
            logger.error("下载照片 无照片信息 服务器返回");
        }
        logger.info("照片下载完成");
    }



    public static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        // 创建一个Buffer字符串
        byte[] buffer = new byte[1024];
        // 每次读取的字符串长度，如果为-1，代表全部读取完毕
        int len = 0;
        // 使用一个输入流从buffer里把数据读取出来
        while ((len = inStream.read(buffer)) != -1) {
            // 用输出流往buffer里写入数据，中间参数代表从哪个位置开始读，len代表读取的长度
            outStream.write(buffer, 0, len);
        }
        // 关闭输入流
        inStream.close();
        // 把outStream里的数据写入内存
        return outStream.toByteArray();
    }
}
